package base;

import javax.persistence.*;
import javax.sql.rowset.serial.SerialBlob;

import utilities.Criterion;

import java.sql.Timestamp;
import java.util.*;
import java.net.InetAddress;
import java.net.UnknownHostException;



public class TestLook {
	
	
	
	static void printFileList(List<File> files){		
		if(files.isEmpty())	
			System.out.println("Empty");
		else{
		for(File f:files)
			System.out.println(f);
		}
		
		
	}
	

	public static void main(String[] arg){
		
		DataTracker d = new DataTracker();
		d.createEm();
		
		
		try{
	
		
		File newfile = new File(12, 25, "11");
		File newfile1 = new File(7, 14, Integer.toString(12));
		newfile.setFilenameTmp("Titi");
		newfile1.setFilenameTmp("Toto");
		
		InetAddress ad = InetAddress.getByName("localhost");
		InetAddress ad1 = InetAddress.getByName("145.25.152.10");
		InetAddress ad2 = InetAddress.getByName("195.125.112.158");
		
		Peer newpeer = new Peer(ad, 2542);
		Thread.currentThread().sleep(1000);
		Peer newpeer1 = new Peer(ad1, 1245);
		Thread.currentThread().sleep(1000);
		Peer newpeer2 = new Peer(ad2, 8002);
		
		d.addSeeder(newfile, newpeer);
		d.addSeeder(newfile1, newpeer1);
		d.addLeecher("12", newpeer2);
		d.addSeeder(newfile, newpeer2);
		
		
		//Affichage de la base
		System.out.println("");
		System.out.println("");
		System.out.println("Base:");
		d.printFiles();
		d.printPeers();
		d.printShares();
		System.out.println("");
		
		
		//Test pour la recherche par nom
		System.out.println("Test: recherche par nom de fichier");
		String request = "";
		request = d.findFileByName(request, "Titi");
		
		System.out.println("Request : " + request);
		
		List<File> files  = d.executeFile(request);
		System.out.println("Result");
		String result = d.responseLook(files);
		System.out.println(result);

		
		
		//Test pour la recherche par taille de fichier
		System.out.println("Test: recherche par taille de fichier");
		String request1 = "";
		request1 = d.findFileByLength(request1, 14, Criterion.Operator.GEQ);
		
		System.out.println("Request : " + request1);
		
		List<File> files1  = d.executeFile(request1);
		System.out.println("Result");
		String result1 = d.responseLook(files1);
		System.out.println(result1);
		
		
		//Test avec les 2 criteres
		System.out.println("Test: recherche par les 2 criteres");
		String request2 = "";
		request2 = d.findFileByName(request2, "Titi");
		request2 = d.findFileByLength(request2, 8, Criterion.Operator.G);
		
		System.out.println("Request : " + request2);
		
		List<File> files2 = d.executeFile(request2);
		System.out.println("Result");
		String result2 = d.responseLook(files2);
		System.out.println(result2);
		d.closeEm();
		d.disconnexionTrakerDB();
		
		
		}
		catch(Exception e){
			e.printStackTrace();
		}
		
		
}
	
	
	
	
}
